//
//  TPCityViewController.m
//  Tripvi
//
//  Created by xiaowuxiaowu on 2017/5/10.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "TPCityViewController.h"
#import "TPCityHeader.h"
#import "TPCityModel.h"
@interface TPCityViewController ()
@property (nonatomic,strong)TPCityHeader *header;
@property (nonatomic,strong)TPCitySouceModel *dataModel;
@property (nonatomic,copy)NSString *city;
@end

@implementation TPCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"位置";
    
    
    [self setup];
}
-(void)setup{
    
     self.city = [XWCommon shareAppDelegate].locationCity;
    _header = [[TPCityHeader alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 0)];
    self.tableView.tableHeaderView = _header;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleDefault;
    self.tableView.sectionIndexColor = TPAllBackgroundColor;
    self.tableView.sectionIndexBackgroundColor = [UIColor whiteColor];
   @weakify(self);
    [_header.searchBar setTextDidChangeBlock:^(UISearchBar *searchBar, NSString *searchText) {
        
        @strongify(self);
        
        self.dataModel.searchText = searchText;
        [self.tableView reloadData];
        DLog(@"---%@",searchText);
    }];
    
    
    self.dataModel = [[TPCitySouceModel alloc]init];
   
   
    
    
    
//    TPButton *sender = [TPButton buttonWithFrame:CGRectZero title:AppLocalString(@"Next") font:fontSystemOfSize(16) color:[UIColor whiteColor]];
//    [sender setImage:ImageNamed(@"Cell_right_arrow_withe") forState:UIControlStateNormal];
//    [sender setBackgroundImageColor:TPAllBackgroundColor forState:UIControlStateNormal];
//    [sender addTouchUpInsideTarget:self action:@selector(toActionButton:)];
//    [self.view addSubview: sender];
//    [sender mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.left.right.mas_equalTo(0.0f);
//        make.height.mas_equalTo(50.0f);
//    }];
//
//     self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50.0, 0);
//
//
////    if(self.isAttractions)
//        [sender setTitle:AppLocalString(@"Done") forState:UIControlStateNormal];
//
    [self getRequest];
    
}
-(void)getRequest{
    
    
    
  [[TPRequestClient sharedClient]getGetAllCity_success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
      
//      NSMutableArray *data = [NSMutableArray new];
//      [[((NSDictionary *)respone.data) allValues] enumerateObjectsUsingBlock:^(id   obj, NSUInteger idx, BOOL * _Nonnull stop) {
//          [data addObjectsFromArray:obj];
//      }];
//
      self.dataModel.sourceArray = respone.data;
      [self.tableView reloadData];
      
  } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
      ;
  } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
      ;
  }];
}

-(void)toActionButton:(UIButton *)sender{
    
    if(self.selectBlock){
        self.selectBlock(self.city);
        [XWCommon shareAppDelegate].locationCity = self.city;
    }
    [self leftBtnAction:nil];
    
//    if(self.isAttractions)
//        return;
//        
//    TPCityViewController *cityVC = [TPCityViewController new];
//    cityVC.isAttractions = YES;
//    [self.navigationController pushViewController:cityVC animated:YES];
}
#pragma mark---tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.dataModel.sectionArray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataModel.sectionArray[section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellx"];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellx"];
        cell.tintColor = TPAllBackgroundColor;
        
    }
    
    TPCityModel *model = self.dataModel.sectionArray[indexPath.section][indexPath.row];
    cell.textLabel.text = model.city;
    cell.accessoryType = [self.city containsString:cell.textLabel.text] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    
    return cell;
}
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//  TPCityModel *model = self.dataModel.sectionArray[section][0];
//    return model.attic;
//}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, tableView.width, 30.0)];
    label.textColor = TPAllBackgroundColor;
    label.backgroundColor = [UIColor colorWithHexString:@"f5f4f5"];
    TPCityModel *model = self.dataModel.sectionArray[section][0];
    label.text = [@"  " stringByAppendingString: model.attic];
    return label;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30.0f;
}
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.dataModel.atticArray;
}
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    if(index == 1){
        [tableView scrollToTopAnimated:YES];
    }
    return index-1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    self.city = cell.textLabel.text;
    [tableView reloadData];
    [self toActionButton:nil];
}

@end
